/*
* $Id: QBeeView.cpp 152 2010-09-23 18:28:55Z volok-aleksej@yandex.ru $
*
* Copyright 2010 by Volokitin Alexey
* All rights reserved
*
*/
#include "Common/include.h"
#include "QBeeView.h"
#include "../../BQManagerDialog.h"
#include "../JournalDefined.h"
#include "../../../UserMessage.h"

#pragma warning(disable: 4407)

#define SIZE_SCROLL		17

#define SB_USER_UP			9
#define SB_USER_DOWN		10

CQBeeView::CQBeeView(CState* pState)
: CEmptyViewJournal(pState), m_infoDrawer(dynamic_cast<IWnd*>(this)){}
CQBeeView::~CQBeeView(){}

BEGIN_MESSAGE_MAP(CQBeeView, CEmptyViewJournal)
	ON_WM_CREATE()
	ON_WM_PAINT()
	ON_WM_SIZE()
	ON_WM_SETCURSOR()
	ON_WM_HSCROLL()
	ON_WM_VSCROLL()
	ON_WM_LBUTTONDOWN()
	ON_WM_LBUTTONUP()
	ON_WM_MOUSEMOVE()
	ON_WM_TIMER()
	ON_WM_DESTROY()
	ON_WM_MOUSEWHEEL()
	ON_MESSAGE(UM_BUTTONCLICK, OnButtonClick)
END_MESSAGE_MAP()

afx_msg void CQBeeView::OnPaint(){
	CPaintDC dc(this);
	CRect rect;
	GetClientRect(rect);
	if(m_bVScroll)
		rect.right -= SIZE_SCROLL;
	if(m_bHScroll)
		rect.bottom -= SIZE_SCROLL;
	m_infoDrawer.Paint(&dc, rect);
	if(m_bHScroll && m_bVScroll){
		CBrush brush;
		brush.CreateSolidBrush(GetSysColor(COLOR_3DFACE));
		dc.FillRect(CRect(rect.right, rect.bottom,
			rect.right + SIZE_SCROLL, rect.bottom + SIZE_SCROLL), &brush);
		brush.DeleteObject();
	}
}

afx_msg void CQBeeView::OnSize(UINT nType, int cx, int cy){
	m_infoDrawer.SetSizeCx(cx);
	UpgradeScroll(cx, cy);
	Invalidate(FALSE);
	__super::OnSize(nType, cx, cy);
}

afx_msg BOOL CQBeeView::OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message){
	SetCursor(LoadCursor(NULL, IDC_ARROW));
	return TRUE;
}

afx_msg int CQBeeView::OnCreate(LPCREATESTRUCT lpCreateStruct){
	int res = __super::OnCreate(lpCreateStruct);
	SetTimer(2, 10, NULL);
	m_HScroll.Create(WS_CHILD | WS_VISIBLE | SBS_HORZ, CRect(), this, HSCROLL_ID);
	m_VScroll.Create(WS_CHILD | WS_VISIBLE | SBS_VERT, CRect(), this, VSCROLL_ID);
	return res;
}

afx_msg void CQBeeView::OnHScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar){
	Scroll(nSBCode, pScrollBar);
}

afx_msg void CQBeeView::OnVScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar){
	Scroll(nSBCode, pScrollBar);
}

afx_msg void CQBeeView::OnLButtonDown(UINT nFlags, CPoint point){
	SetFocus();
	if(m_infoDrawer.ProcessMouse(WM_LBUTTONDOWN, point, 0))
		Invalidate(FALSE);
	__super::OnLButtonDown(nFlags, point);
}

afx_msg void CQBeeView::OnLButtonUp(UINT nFlags, CPoint point){
	if(m_infoDrawer.ProcessMouse(WM_LBUTTONUP, point, 0))
		Invalidate(FALSE);
	__super::OnLButtonUp(nFlags, point);
}

afx_msg void CQBeeView::OnMouseMove(UINT nFlags, CPoint point){
	if(m_infoDrawer.ProcessMouse(WM_MOUSEMOVE, point, 0))
		Invalidate(FALSE);
	__super::OnMouseMove(nFlags, point);
}

afx_msg void CQBeeView::OnTimer(UINT_PTR nIDEvent){
	CPoint pos;
	GetCursorPos(&pos);
	CRect rect;
	GetWindowRect(rect);
	CSize size = pos - rect.TopLeft();
	if(!rect.PtInRect(pos) && m_infoDrawer.ProcessMouse(WM_MOUSELEAVE, CPoint(size.cx, size.cy), 0))
		Invalidate(FALSE);
}

afx_msg BOOL CQBeeView::OnMouseWheel(UINT nFlags, short zDelta, CPoint pt){
	if(m_bVScroll)
		OnVScroll(zDelta > 0 ? SB_USER_UP : SB_USER_DOWN, 0, &m_VScroll);
	return __super::OnMouseWheel(nFlags, zDelta, pt);
}

afx_msg void CQBeeView::OnDestroy(){
	KillTimer(2);
}

afx_msg LRESULT CQBeeView::OnButtonClick(WPARAM wParam, LPARAM lParam){
	CBQManagerDialog::type tp;
	if(m_pState->m_nState == Journal::VIEW_POINT_QBEE)
		tp = CBQManagerDialog::POINT;
	else if(m_pState->m_nState == Journal::VIEW_FAMILY_QBEE)
		tp = CBQManagerDialog::FAMILY;
	else if(m_pState->m_nState == Journal::VIEW_INSPECT_QBEE)
		tp = CBQManagerDialog::QUEEN_BEE;
	CBQManagerDialog qbManager(this, tp, m_infoDrawer.GetId(m_infoDrawer.GetSel()));
	qbManager.DoModal();
	return 1;
}

void CQBeeView::UpgradeScroll(int cx, int cy)
{
	int sizeCx = cx, sizeCy = cy;
	int sizeViewY = m_infoDrawer.GetSizeY();
	bool bResizeCy = false;
	if(sizeCx < m_infoDrawer.GetMinWidth()){
		sizeCy -= SIZE_SCROLL;
		bResizeCy = true;
	}
	if(sizeCy < sizeViewY){
		sizeCx -= SIZE_SCROLL;
		m_infoDrawer.SetSizeCx(sizeCx);
		sizeViewY = m_infoDrawer.GetSizeY();
	}
	if(sizeCx < m_infoDrawer.GetMinWidth() && !bResizeCy)
		sizeCy -= SIZE_SCROLL;
	if(sizeCy < sizeViewY)
		m_bVScroll = true;
	else
		m_bVScroll = false;
	if(cx < m_infoDrawer.GetMinWidth())
		m_bHScroll = true;
	else
		m_bHScroll = false;

	if(m_bHScroll)
		m_HScroll.ShowWindow(SW_SHOW);
	else
		m_HScroll.ShowWindow(SW_HIDE);
	if(m_bVScroll)
		m_VScroll.ShowWindow(SW_SHOW);
	else
		m_VScroll.ShowWindow(SW_HIDE);

	if(m_bHScroll){
		SCROLLINFO si;
		m_HScroll.GetScrollInfo(&si);
		si.nPage = sizeCx;
		si.nMin = 0;
		si.nMax = m_infoDrawer.GetMinWidth();
		m_HScroll.SetScrollInfo(&si);
		m_HScroll.GetScrollInfo(&si);
		m_infoDrawer.SetBeginPosX(-si.nPos);
	}
	if(m_bVScroll){
		SCROLLINFO si;
		m_VScroll.GetScrollInfo(&si);
		si.nPage = sizeCy;
		si.nMin = 0;
		si.nMax = sizeViewY;
		m_VScroll.SetScrollInfo(&si);
		m_VScroll.GetScrollInfo(&si);
		m_infoDrawer.SetBeginPosY(-si.nPos);
	}
	else{
		SCROLLINFO si;
		m_VScroll.GetScrollInfo(&si);
		si.nPos = 0;
		m_VScroll.SetScrollInfo(&si);
		m_infoDrawer.SetBeginPosX(0);
		m_infoDrawer.SetBeginPosY(0);
	}
	MoveScroll(cx, cy);
}

void CQBeeView::MoveScroll(int sizeCx, int sizeCy)
{
	CRect rectHScroll(0, sizeCy - SIZE_SCROLL,	sizeCx, sizeCy);
	CRect rectVScroll(sizeCx - SIZE_SCROLL, 0, sizeCx, sizeCy);
	if(m_bVScroll)
		rectHScroll.right -= SIZE_SCROLL;
	m_HScroll.MoveWindow(rectHScroll, TRUE);
	if(m_bHScroll)
		rectVScroll.bottom -= SIZE_SCROLL;
	m_VScroll.MoveWindow(rectVScroll, TRUE);
}

void CQBeeView::Scroll(UINT nSBCode, CScrollBar* pScrollBar){
	SCROLLINFO si;
	pScrollBar->GetScrollInfo(&si);
	if(nSBCode == SB_THUMBTRACK){
		si.nPos = si.nTrackPos;
	} else if(nSBCode == SB_LINEDOWN){
		si.nPos += 10;
	} else if(nSBCode == SB_LINEUP){
		si.nPos -= 10;
	} else if(nSBCode == SB_PAGEDOWN){
		si.nPos += si.nPage;
	} else if(nSBCode == SB_PAGEUP){
		si.nPos -= si.nPage;
	}else if(nSBCode == SB_USER_UP){
		si.nPos -= 30;
	}else if(nSBCode == SB_USER_DOWN){
		si.nPos += 30;
	}
	pScrollBar->SetScrollInfo(&si);
	pScrollBar->GetScrollInfo(&si);
	if(pScrollBar == &m_VScroll)
		m_infoDrawer.SetBeginPosY(-si.nPos);
	else if(pScrollBar == &m_HScroll)
		m_infoDrawer.SetBeginPosX(-si.nPos);
	Invalidate(FALSE);
}
